<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./reset.css">
    <link rel="stylesheet" href="./shopping.css">
</head>

<body>
    <div class="bigBox">
        <table class="one">
            <tr>
                <th><input type="checkbox" name='chkAll' onclick="chkAll(this)">全选</th>
                <th>商品图片</th>
                <th>数量</th>
                <th>单价</th>
                <th>商品名称</th>
                <th>小计</th>
                <th>操作</th>
            </tr>
            <tr>
                <td><input type="checkbox" name='chkb' onclick='chk_o(this)'></td>
                <td><img src="./img/1.jpg"></td>
                <td><button onclick='subNum(this)'>-</button><input type="text" required placeholder="1"><button onclick='addNum(this)'>+</button></td>
                <td>$20000.50</td>
                <td>植物活力</td>
                <td>$20000.50</td>
                <td><button onclick='del(this)'>删除</button></td>
            </tr>
            <tr>
                <td><input type="checkbox" name='chkb' onclick='chk_o(this)'></td>
                <td><img src="./img/2.jpg"></td>
                <td><button onclick='subNum(this)'>-</button><input type="text" required placeholder="1"><button onclick='addNum(this)'>+</button></td>
                <td>$20000.50</td>
                <td>植物活力2</td>
                <td>$20000.50</td>
                <td><button onclick='del(this)'>删除</button></td>
            </tr>
        </table>
        <div class="operate">
            <button onclick="delAll(this)">删除已选</button>
            <button onclick="addtr()">增加一行</button>
            <span>总计</span>
            <i class="price" id="price">$40001.00</i>
        </div>
    </div>
    <script>
        var i = 0;
        var arr = [3, 4, 5, 6, 7];
        var arr1 = ['植物活力3', "植物活力4", "植物活力5", "植物活力6", "植物活力7"]
        var tab = document.getElementsByTagName("table")[0];
        var tbody = tab.firstElementChild;

        function addtr() {
            var last1 = tbody.lastElementChild;
            var newTr = document.createElement("tr")
            newTr.innerHTML = "<td><input type='checkbox' name='chkb' onclick='chk_o(this)'></td>" + "<td><img src='./img/" + arr[i] + ".jpg'></td>" + "<td><button onclick='subNum(this)'>-</button><input type='text' required placeholder='1'><button onclick='addNum(this)'>+</button></td><td>$20000.50</td>" + "<td>" + arr1[i] + "</td>" + "<td>$20000.50</td><td><button onclick='del(this)'>删除</button></td>";
            i++;
            if (i == arr1.length) {
                i = 0;
            }
            tbody.appendChild(newTr);
            var zongji = document.getElementById("price");
            var money = zongji.innerText;
            money = money.slice(1);
            zongji.innerText = "$" + String(Number(money) + 20000.50);
            var chkall = document.getElementsByName("chkAll");
            if (chkall[0].checked == true) {
                chkall[0].checked = false;
            }

        }

        function del(btn) {
            var tr1 = btn.parentNode.parentNode;
            tbody.removeChild(tr1);
            var chks = document.getElementsByName("chkb");
            var chkall = document.getElementsByName("chkAll");
            var j = 1;
            for (var i = 0; i < chks.length; i++) {
                if (chks[i].checked == false) {
                    j = 2;
                    break;
                }
            }
            if (j == 1) {
                chkall[0].checked = true;
            }
        }

        function chkAll(obj) {
            var chks = document.getElementsByName("chkb");
            for (var i = 0; i < chks.length; i++) {
                chks[i].checked = obj.checked;
            }
        }

        function chk_o(obj) {
            var chks = document.getElementsByName("chkb");
            var chkall = document.getElementsByName("chkAll");
            if (obj.checked == false) {
                chkall[0].checked = false;
            } else {
                var j = 1;
                for (var i = 0; i < chks.length; i++) {
                    if (chks[i].checked == false) {
                        j = 2;
                        break;
                    }
                }
                if (j == 1) {
                    chkall[0].checked = true;
                }
            }

        }

        function addNum(btn) {
            var num = btn.previousElementSibling;
            var val = num.placeholder;
            val++;
            num.placeholder = val;
            var xiaoji = btn.parentNode.parentNode.lastElementChild.previousElementSibling;
            var price = xiaoji.innerText;
            price = price.slice(1);
            xiaoji.innerText = "$" + String(Number(price) + 20000.50);
            var zongji = document.getElementById("price");
            var money = zongji.innerText;
            money = money.slice(1);
            zongji.innerText = "$" + String(Number(money) + 20000.50);
        }

        function subNum(btn) {
            var num = btn.nextElementSibling;
            var val = num.placeholder;
            if (val > 0) {
                val--;
            }
            num.placeholder = val;
            var xiaoji = btn.parentNode.parentNode.lastElementChild.previousElementSibling;
            var price = xiaoji.innerText;
            price = price.slice(1);
            if (price > 0) {
                xiaoji.innerText = "$" + (String(Number(price) - 20000.5));
            }
            var zongji = document.getElementById("price");
            var money = zongji.innerText;
            money = money.slice(1);
            if (money > 0 && price > 0) {
                zongji.innerText = "$" + String(Number(money) - 20000.50);
            }
        }

        function delAll(btn) {
            var last2 = tbody.lastElementChild;
            var num = tbody.childElementCount - 1;
            var chk = document.getElementsByName('chkb');
            for (var i = chk.length - 1; i >= 0; i--) {
                if (chk[i].checked) {
                    var xiaoji = chk[i].parentNode.parentNode.lastElementChild.previousElementSibling;
                    var price = xiaoji.innerText;
                    price = price.slice(1);
                    var zongji = document.getElementById("price");
                    var money = zongji.innerText;
                    money = money.slice(1);
                    zongji.innerText = "$" + String(Number(money) - Number(price));
                    var chkall = document.getElementsByName("chkAll");
                    console.log(chkall[0].checked);
                    if (chkall[0].checked) {
                        chkall[0].checked = false;
                    }
                    tbody.removeChild(chk[i].parentNode.parentNode);
                }
            }
        }
    </script>

</body>

</html>